<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Scenicspot extends Base{
    public function spots()
    {
        if(!empty(input("param.latitude"))){
            $latitude=input("param.latitude");
        }else{
            return json(['code' => 501, 'msg' => '纬度呢']);
        }
        if(!empty(input("param.longitude"))){
            $longitude=input("param.longitude");
        }else{
            return json(['code' => 502, 'msg' => '经度呢']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 503, 'msg' => '当前分页数']);
        }
        $pagef=10*($page-1);
        if(input("param.flag_type")==1){//优选景区
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 order by stars desc) as a where a.distance<=5000000 limit ".$pagef.",10";
        }elseif(input("param.flag_type")==2){//免费景区
            // $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 order by price) as a where a.distance<=5000000 limit ".$pagef.",10";
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and price=0 order by id desc) as a where a.distance<=5000000 limit ".$pagef.",10";
        }elseif(input("param.flag_type")==3){//距离近->远
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 order by distance) as a where a.distance<=5000000 limit ".$pagef.",10";
        }else{//全部
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 order by id desc) as a where a.distance<=5000000 limit ".$pagef.",10";
        }
        $res=Db::query($sql);
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
        
    }
    public function spotsSearch()
    {
        if(!empty(input("param.latitude"))){
            $latitude=input("param.latitude");
        }else{
            return json(['code' => 501, 'msg' => '纬度呢']);
        }
        if(!empty(input("param.longitude"))){
            $longitude=input("param.longitude");
        }else{
            return json(['code' => 502, 'msg' => '经度呢']);
        }
        if(!empty(input("param.keyword"))){
            $keyword=input('param.keyword');
        }else{
            return json(['code' => 503, 'msg' => '搜索关键词呢']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);
        }
        $pagef=10*($page-1);
        if(input("param.flag_type")==1){//优选景区
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title like '%".$keyword."%' order by stars desc) as a where a.distance<=5000000 limit ".$pagef.",10";
        }elseif(input("param.flag_type")==2){//免费景区
            // $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title like '%".$keyword."%' order by price) as a where a.distance<=5000000 limit ".$pagef.",10";
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and price=0 and title like '%".$keyword."%' order by id desc) as a where a.distance<=5000000 limit ".$pagef.",10";
        }elseif(input("param.flag_type")==3){//距离近->远
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title like '%".$keyword."%' order by distance) as a where a.distance<=5000000 limit ".$pagef.",10";
        }else{//全部
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title like '%".$keyword."%' order by id desc) as a where a.distance<=5000000 limit ".$pagef.",10";
        }
        $res=Db::query($sql);
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
        
    }

}